/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.SrvCureplan;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author Munliga
 */
@Stateless
public class SrvCureplanFacade extends AbstractFacade<SrvCureplan> {
    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public SrvCureplanFacade() {
        super(SrvCureplan.class);
    }
//Create Query
    public List<SrvCureplan> LoadDataCurePlan(String txtName,
            String txtPrice,
            String txtCurrency,
            String txtDoctorName,
            String ddlStatus,
            String sortIndex,
            String sortDirection)
    {
        try {
            
            String strQuery = "SELECT s FROM SrvCureplan s ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtName.isEmpty()) ? " AND s.srvCureservice.name Like '%" + txtName + "%' " : " ";
            strWhere += (!txtPrice.isEmpty()) ? " AND s.srvCureservice.price <= " + Integer.parseInt(txtPrice) + " " : " ";
            strWhere += (!txtCurrency.isEmpty()) ? " AND s.srvCureservice.currency Like '%" + txtCurrency + "%' " : " ";
            strWhere += (!txtDoctorName.isEmpty()) ? " AND s.srvCureservice.mDoctor.name Like '%" + txtDoctorName + "%' " : " ";
            strWhere += (!ddlStatus.isEmpty()) ? " AND s.status = '" + ddlStatus + "' " : " ";
            strWhere += "ORDER BY s." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }

    }
        
    public List<SrvCureplan> LoadDataCurePlanProvider(String txtName_User,
            String txtName_CureService,
            String ddlStatus,
            String txtDoctorID,
            String sortIndex,
            String sortDirection) {
        try {
            String strQuery = "SELECT A FROM SrvCureplan A ";
            String strWhere = " Where 1=1 AND A.status <> 'D' ";
            strWhere += (!txtName_User.isEmpty()) ? " AND Concat(A.mUser.name,' ',A.mUser.surname) Like '%" + txtName_User + "%' " : " ";
            strWhere += (!txtName_CureService.isEmpty()) ? " AND A.srvCureservice.name Like '%" + txtName_CureService + "%' " : " ";
            strWhere += (!ddlStatus.isEmpty()) ? " AND A.status = '" + ddlStatus + "' " : " ";
            strWhere += (!txtDoctorID.isEmpty()) ? " AND A.srvCureservice.mDoctor.id = " + Integer.parseInt(txtDoctorID) + " " : " ";
            strWhere += "ORDER BY A." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();


        } catch (Exception e) {
            return null;
        }

    }

    public List<SrvCureplan> LoadDataCurePlanByUserId(String id, String txtKeyword, String txtPrice, String ddlStatus, String sortIndex,String sortDirection)
    {
        try {
            String strQuery = "SELECT s FROM SrvCureplan s ";
            String strWhere = " Where 1=1 ";
            strWhere += (!id.isEmpty()) ? " AND s.mUser.id = " + Long.parseLong(id) + " " : " ";
            strWhere += (!txtKeyword.isEmpty()) ? " AND CONCAT(s.srvCureservice.name,' ',s.srvCureservice.currency,' ',s.srvCureservice.mDoctor.name,' ',s.srvCureservice.mDoctor.surname,' ',s.srvCureservice.mHospital.name) Like '%" + txtKeyword + "%' " : " ";
            strWhere += (!txtPrice.isEmpty()) ? " AND s.srvCureservice.price <= " + Integer.parseInt(txtPrice) + " " : " ";
            strWhere += (!ddlStatus.isEmpty()) ? " AND s.status = '" + ddlStatus + "' " : " ";
            strWhere += "ORDER BY s." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }

    }

}




